<?php
/**
 * This file is part of Task Agile.
 * Task Agile is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License.
 * Task Agile is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Task Agile.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category   TaskAgile
 * @package    Controller
 * @copyright  Copyright (c) 20010-2010 Estrada Virtual (http://estradavirtual.com.br)
 * @license    http://www.gnu.org/licenses/gpl.txt GPLv3
 * @version    $Id$
*/
/**
 * Model de Task
 * @filesource  01/08/2010
 * @author      Estrada Virtual <http://estradavirtual.com.br>
 * @copyright   Copyright <2010> Estrada Virtual
 * @package     <<application>>
 * @subpackage  <<application>>.application.models
 * @version     $Id$
 */
class Task extends Tev_Db_Table
{
    const STATUS_ACTIVE   = 'A';
    const STATUS_INACTIVE = 'I';
    const STATUS_CANCELED = 'C';
    const STATUS_FINISHED = 'F';

    const PRIORITY_LOWER  = 100;
    const PRIORITY_MEDIUM = 50;
    const PRIORITY_HIGH   = 10;

    const READ     = 'T';
    const NOT_READ = 'F';

    protected $_name             = 'task';
    protected $_dependentTables  = array('TaskComment');
    protected $_primary          = array('id');

    protected $_referenceMap = array(
                    'Project' => array(
                            'columns'       => 'fk_project',
                            'refTableClass' => 'Project',
                            'refColumns'    => 'id',
                         ),
                    'User' => array(
                            'columns'       => 'fk_user',
                            'refTableClass' => 'User',
                            'refColumns'    => 'id',
                         ));

     public function getStatus()
     {
         return array(''					=> 'Selecione',
         			  Task::STATUS_ACTIVE 	=> 'Ativo',
                      Task::STATUS_INACTIVE => 'Inativo',
                      Task::STATUS_CANCELED => 'Cancelado',
                      Task::STATUS_FINISHED => 'Finalizado');
     }
    /**
     * Return array of prioritys
     * @return array
     */
    public function getPrioritys()
    {
        return array(''			     	   => 'Selecione',
         			 Task::PRIORITY_LOWER  => 'Baixa',
                     Task::PRIORITY_MEDIUM => 'Média',
                     Task::PRIORITY_HIGH   => 'Alta');
    }

    public function close($idTask, $idUser)
    {
        // @todo tratar caso não encontre registro
        $rowTask = $this->find($idTask)->current();

        /*
        if ($idUser != $rowTask->fk_user) {
            // @todo colocar msg
            $this->_redirect('task/dashboard');
        }
        if (Task::STATUS_FINISHED == $rowTask->status) {
            // @todo colocar msg
            throw new Tev_Ex('msg');
        }
        */

        $rowTask->status    = Task::STATUS_FINISHED;
        $rowTask->dt_finish = date('Y-m-d H:i:s');
        $rowTask->save();

        // Comment
        $rowUser = $rowTask->findParentUser();
        $translate = Zend_Registry::get('Zend_Translate');
        $comment = $translate->_('Task closed by') . ' ' . $rowUser->name;
        $taskComment = new TaskComment();
        $taskComment->comment($idTask, $idUser, $comment, null, TaskComment::TYPE_CLOSED);
        return $rowTask;
    }

    public function open($idTask, $idUser)
    {
        // @todo tratar caso não encontre registro
        $rowTask = $this->find($idTask)->current();
        if ($idUser != $rowTask->fk_user) {
            // @todo colocar msg
            //throw new Tev_Ex('msg');
        }
        if (Task::STATUS_ACTIVE == $rowTask->status) {
            // @todo colocar msg
            //throw new Tev_Ex('msg');
        }

        $rowTask->status    = Task::STATUS_ACTIVE;
        $rowTask->dt_finish = null;
        $rowTask->save();

        // Comment
        $rowUser = $rowTask->findParentUser();
        $translate = Zend_Registry::get('Zend_Translate');
        $comment = $translate->_('Task open by') . ' ' . $rowUser->name;
        $taskComment = new TaskComment();
        $taskComment->comment($idTask, $idUser, $comment, null, TaskComment::TYPE_OPENED);
        return $rowTask;
    }

    public function forward($idTask, $idUser, $rowUserTo)
    {
        $rowTask          = $this->find($idTask)->current();
        $rowTask->fk_user = $rowUserTo->id;
        $rowTask->read    = Task::NOT_READ;
        $rowTask->save();

        // Comment
        $translate = Zend_Registry::get('Zend_Translate');
        $comment = $translate->_('Task sent to') . ' ' . $rowUserTo->name;
        $taskComment = new TaskComment();
        $taskComment->comment($idTask, $idUser, $comment, null, TaskComment::TYPE_FORWARD);
        return $rowTask;
    }

    public function inactivate($idTask)
    {
        $rowTask          = $this->find($idTask)->current();
        $rowTask->read    = Task::NOT_READ;
        $rowTask->status  = Task::STATUS_INACTIVE;
        $rowTask->save();

        $translate = Zend_Registry::get('Zend_Translate');
        $comment = $translate->_('Task inactivate');
        $taskComment = new TaskComment();
        $taskComment->comment($idTask, $rowTask->fk_user, $comment, null, TaskComment::TYPE_INACTIVATED);
        return $rowTask;
    }
}
